/**
 * 资源类
 */
import apiUrl from "@/config/settings";

/**
 *
 * 资源图片外链处理
 * @param {*} url
 * @return {*}
 */
const mediaImage = (url) => {
  if (!url) {
    return;
  }

  return `${apiUrl.config.attachment_url}${url}`;
};

/**
 * 拼接路由及参数
 * @param routes
 * @param params
 * @returns {*}
 */
function getUrl(routes, params) {
  let settings = apiUrl.config;
  let url = routes;
  let base_url = settings?.base_url || "";
  if (params && typeof params === "object") {
    let string = "";
    for (let item in params) {
      string += item + "=" + params[item] + "&";
    }
    url += ((base_url + url).indexOf("?") === -1 ? "?" : "&") + string;
  }
  if (/.+\?$/.test(url)) {
    //？结尾的去掉？
    url = url.slice(0, -1);
  }
  return (settings?.base_url || "") + url;
}

export { mediaImage, getUrl };
